package cn.wolfcode.shop.cloud.web.controller;


import cn.wolfcode.shop.cloud.common.Result;
import cn.wolfcode.shop.cloud.domain.Good;
import cn.wolfcode.shop.cloud.redis.RedisService;
import cn.wolfcode.shop.cloud.redis.SeckillKeyPrefix;
import cn.wolfcode.shop.cloud.service.ISeckillGoodService;
import cn.wolfcode.shop.cloud.vo.SeckillGoodVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/seckillGood")
public class SeckillGoodController {

    @Autowired
    private ISeckillGoodService seckillGoodService;
    @Autowired
    private RedisService redisService;

    /**
     * 500 * 10 = 1700
     * @return
     */
    @RequestMapping("/query")
    public Result<List<SeckillGoodVo>> query(){
        return Result.success(seckillGoodService.queryCache());
    }

    @RequestMapping("/getSeckillGoodDetail")
    public Result<SeckillGoodVo>getSeckillGoodDetail(Long seckillId){
        return Result.success(seckillGoodService.findByIdCache(seckillId));
    }
    @RequestMapping("/initData")
    public Result<String> initData(){
        List<SeckillGoodVo> list = seckillGoodService.query();
        for (SeckillGoodVo seckillGoodVo : list) {
            redisService.set(SeckillKeyPrefix.SECKILL_STOCK,seckillGoodVo.getId()+"",seckillGoodVo.getStockCount());
            redisService.hset(SeckillKeyPrefix.SECKILL_GOOD_HASH,"",seckillGoodVo.getId()+"",seckillGoodVo);
        }
        return Result.success("初始化成功");
    }
}
